package com.glory.dgcpcpda.activity.mfg;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;

public class LotToolInHouseActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;

    @BindView(R.id.elResult)
    TextView elResult;

    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etUserName)
    FreeUI_ClearEditText etUserName;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_tool_in;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setVisibility(View.GONE);
        setMainLayout(llMain);

        tvTitle.setText("物料治具入库");

        initEdit(etLot, s -> {
            etLot.setText("");
            new Handler().postDelayed(() -> etLot.requestFocus(),500);

            String StorgeId = getText(etUserName);
            if ("".equals(StorgeId)){
                showToast("请先扫描货架号！",false);
                etLot.setFocusable(true);
                return;
            }
            Submit(s, StorgeId);
        });
    }

    @Override
    protected void initData() {

    }

    private  void  Submit(String MLOTID, String STORAGEID){
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), LotToolInHouseActivity.this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    String result = response.body().getResponse().getHeader().getRESULTMESSAGE();
                    if("".equals(result)){
                        result = "入库成功！！！";
                    }
                    showToast(result,true);
                    etLot.setText("");
                    etLot.setFocusable(true);
                    elResult.setText(MLOTID + " " + result);
                }else{
                    elResult.setText(response.body().getResponse().getHeader().getRESULTMESSAGE());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(),"RECEIVEMLOT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("MLOTID",MLOTID);
        map.addProperty("STORAGEID",STORAGEID);
        NetUtils.requestPostNet("CPC.RECEIVEMLOT", "TRANSFERMES",LotToolInHouseActivity.this,map,callBack);
    }
}